package app;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import durak.DurakCard;
import durak.DurakRule;
import durak.Player;

public class ConsolePlayer extends Player{

	@Override
	public DurakCard onAttack() {

		System.out.println("You can attack with cards : ");
		for (DurakCard card : mHand)
			if (DurakRule.canAttack(card, mTable))
				System.out.println("[" + mHand.indexOf(card) + "] :"
						+ card.toString());

		System.out.println("Attack with  : ");

		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String attackIndex = null;
		do {
			try {
				attackIndex = br.readLine();
				// br.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		} while (!DurakRule.canAttack(mHand.get(Integer.parseInt(attackIndex)),
				mTable));

		DurakCard attackCard = mHand.get(Integer.parseInt(attackIndex));
		mHand.remove(attackCard);

		return attackCard;
	}

	@Override
	public DurakCard[] onDefend() {
		System.out.println("Unresolved cards :");

		for (DurakCard card : mTable.getUnresolvedCards())
			System.out.println("[" + mTable.getUnresolvedCards().indexOf(card)
					+ "] :" + card.toString());

		System.out.println("In your hand :");
		for (DurakCard card : mHand)
			System.out.println("[" + mHand.indexOf(card) + "] :"
					+ card.toString());

		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		int defendIndex = -1;
		int attackIndex = -1;

		do {
			try {

				System.out.println("Attack card index :");
				attackIndex = Integer.parseInt(br.readLine());

				System.out.println("Defend card index :");
				defendIndex = Integer.parseInt(br.readLine());

				// br.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		} while (!DurakRule.canDefend(
				mTable.getUnresolvedCards().get(attackIndex),
				mHand.get(defendIndex), mTable));

		DurakCard defendCard = mHand.get(defendIndex);
		mHand.remove(defendCard);

		DurakCard[] result = { mTable.getUnresolvedCards().get(attackIndex),
				defendCard };
		return result;
	}
	

}
